
import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
//import 'package:shared_preferences/shared_preferences.dart'; //数据持久化
import '../provide/cart.dart';

//页面widget
import './cart_page_widget/cart_item.dart';
import './cart_page_widget/cart_bottom.dart';


class CartPage extends StatelessWidget {
  const CartPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('购物车'),centerTitle: true),
      body: FutureBuilder(
        future: _getCardInfo(context),
        builder: (context,snapshot){
          if(snapshot.hasData){
            List cartList = Provide.value<CartProvide>(context).cardList;
            print(cartList);
            return Column(
             children: <Widget>[
               Expanded(
                 child: Provide<CartProvide>(
                   builder: (context,child,cartProvide){
                     cartList = Provide.value<CartProvide>(context).cardList; //重新获取
                     return ListView.builder(
                        itemCount: cartList.length,
                        itemBuilder: (context,index){
                            return CartItem(cartList[index]);
                        },
                     );
                   },
                 )
               ),
               CardBottom()
             ],
            );
          }else{
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }


  Future<String> _getCardInfo(context) async {
    await Provide.value<CartProvide>(context).getCardInfo();

    return '购物车数据已拿到';
  }
}
